package com.aldobucchi.x.flash.geom
{


	import flash.geom.Point;


	public class GeomUtilTest extends AbstractGeomTest
	{


		
		public function test_getNewOriginAfterRegularTransformationAroundFixedAnchor( ):void
		{
			
			var anchor:Point;
			
			// when the anchor is the origin it should never change
			anchor = new Point( 0,0 );
			t( 	0, 		1, 		anchor, 	new Point( 0,0 ) );		
			t( 	3, 		3, 		anchor, 	new Point( 0,0 ) );		
			t( 	-3, 	0.1, 	anchor, 	new Point( 0,0 ) );		
			t( 	10000, 	0.5, 	anchor, 	new Point( 0,0 ) );		
		
		
			// when there are no operations, nothing should change
			t( 	0, 		1, 		new Point( 0,0 ), 		new Point( 0,0 ) );		
			t( 	0, 		1, 		new Point( 1,1 ), 		new Point( 0,0 ) );		
			t( 	0, 		1, 		new Point( -1,1 ), 		new Point( 0,0 ) );		
			t( 	0, 		1, 		new Point( -1,-11 ), 	new Point( 0,0 ) );		


			// when there are small operations, origin should be similar
			t( 	0.00001,	1, 			new Point( 1,1 ),		new Point( 0,0 ) );		
			t( 	0.00001,	1.000001, 	new Point( 1,1 ),		new Point( 0,0 ) );		
		
		
		
		
			/////////// some easy to calculate anchors
			////////////////////////////////////////////
		
			////// with anchor 1,0
			anchor = new Point( 1,0 );
			t( 	0, 		1, 		anchor, 	new Point( 0,0 ) );	
			// mod scale
			t( 	0, 		2, 		anchor, 	new Point( -1, 0 ) );	
			t( 	0, 		0.5,	anchor, 	new Point( 0.5, 0 ) );	
			
			// rotate 90°
			t( 	Math.PI/2, 	1,	anchor, 	new Point( 1, -1 ) );
				
			// rotate 180°
			t( 	Math.PI, 	1,	anchor, 	new Point( 2, 0 ) );	
			
			// rotate 270°
			t( 	Math.PI * 1.5, 	1,	anchor,	new Point( 1, 1 ) );	

			// rotate 360°
			t( 	Math.PI * 2, 	1,	anchor,	new Point( 0, 0 ) );	



			////// with anchor 
			anchor = new Point( 0,1 );
			t( 	0, 		1, 		anchor, 	new Point( 0,0 ) );	
			// mod scale
			t( 	0, 		2, 		anchor, 	new Point( 0, -1 ) );	
			t( 	0, 		0.5,	anchor, 	new Point( 0, 0.5 ) );	
			
			// rotate 90°
			t( 	Math.PI/2, 	1,	anchor, 	new Point( 1, 1 ) );
				
			// rotate 180°
			t( 	Math.PI, 	1,	anchor, 	new Point( 0, 2 ) );	
			
			// rotate 270°
			t( 	Math.PI * 1.5, 	1,	anchor,	new Point( -1, 1 ) );	

			// rotate 360°
			t( 	Math.PI * 2, 	1,	anchor,	new Point( 0, 0 ) );	
			
			
			
		
		
		}



		private function t( rotation:Number, scale:Number, anchor:Point, expectedOrigin:Point ):void
		{
			var o:Point = GeomUtil.getNewOriginAfterRegularTransformationAroundFixedAnchor( rotation, scale, anchor );
			assertPointsSimilar( o, expectedOrigin );
		}






		public function test_getTranslation( ):void
		{


		 	var p1:Point, p2:Point, trans:Point;
		 
			p1 = new Point( 1, 1 );
			p2 = new Point( 1, 2 );

			trans = GeomUtil.getTranslation( 0, 1, p1, p2 );
			
				assertPointsSimilar( trans, new Point( 0, 1 ) );
			
			

			p1 = new Point( 1, 0 );
			p2 = new Point( 1, 0 );
			
			trans = GeomUtil.getTranslation( 0, 2, p1, p2 );

				assertPointsSimilar( trans, new Point( -1, 0 ) );



			///////////////////////////////////////////////////////
			// border case: when the anchor is the origin
			///////////////////////////////////////////////////////

			p1 = new Point( 0, 0 );
			p2 = new Point( 1, 1 );
			
			trans = GeomUtil.getTranslation( 0, 1, p1, p2 );

				assertPointsSimilar( trans, new Point( 1, 1 ) );

			///////////////////////////////////////////////////////
			// border case: no changes
			///////////////////////////////////////////////////////

			p1 = new Point( 7, -7 );
			p2 = new Point( 7, -7 );
			
			trans = GeomUtil.getTranslation( 0, 1, p1, p2 );

				assertPointsSimilar( trans, new Point( 0, 0 ) );


			///////////////////////////////////////////////////////
			// using a fixed anchor at 1,1
			///////////////////////////////////////////////////////

			p1 = new Point( 1, 1 );
			p2 = new Point( 1, 1 );
			


			// scale 2
			
			trans = GeomUtil.getTranslation( 0, 2, p1, p2 );

				assertPointsSimilar( trans, new Point( -1, -1 ) );



			// rotate 90 degrees
			
			trans = GeomUtil.getTranslation( Math.PI/2 , 1, p1, p2 );

				assertPointsSimilar( trans, new Point( 2, 0 ) );
				
			
				
			// rotate 180 degrees
			
			trans = GeomUtil.getTranslation( Math.PI , 1, p1, p2 );

				assertPointsSimilar( trans, new Point( 2, 2 ) );
				
				

		
		}
		

	}

}